import com.ruoyi.RuoYiApplication;
import com.ruoyi.spot.constants.SpotMonitorConstants;
import com.ruoyi.spot.openapi.createNewOrder.controller.SpotOpenapiCreateNewOrderController;
import com.ruoyi.util.SignUtil;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

/**
 * @program: ChainUp_Monitor_Rear
 * @description:
 * @author: Saumell
 * @create: 2024-07-31 16:15
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class test0817_POST {

    /**
     * OpenAPI
     */

    @Autowired
    private SpotOpenapiCreateNewOrderController spotOpenapiCreateNewOrderController;

    @Test
    public void test1(){
        spotOpenapiCreateNewOrderController.SpotOpenapiNewOrder();
    }

    @Test
    public void test2() throws Exception {
        SignUtil signUtil = new SignUtil();
        String timemills = String.valueOf(System.currentTimeMillis());
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        String bodymessage ="{\"symbol\":\"BTCUSDT\",\"volume\":0.01,\"side\":\"BUY\",\"type\":\"MARKET\",\"newClientOrderId\":\"PC\"}";
        okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, bodymessage);
        String responsestring ;
        String sign = signUtil.toSign(timemills, "POST", SpotMonitorConstants.SPOT_OPENAPI_ORDER_Url, "", bodymessage, "a5eb414c5b98d8c1dc3a0ffb33aae3ab");
        try {
            Request request = new Request.Builder()
                    .url(SpotMonitorConstants.SPOT_OPENAPI_ORDER)
                    .method("POST", body)
                    .addHeader("X-CH-APIKEY", "cf5b853b8ea346dab6bfde9b3269c55f")
                    .addHeader("X-CH-TS", timemills)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("X-CH-SIGN",sign)
                    .build();

            Response response = client.newCall(request).execute();
            responsestring = response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(responsestring);
    }
}
